package com.my.service.impl;


import com.my.service.OrdersService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Resource
    private LoadBalancerClient loadBalancerClient;//ribbon负载均衡器

    private String getUrl(String str) {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("orders-provider");
        return "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/orders/" + str;
    }

    @Override
    public String sendOrders(Integer ordersId, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("ordersId", ordersId);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("send"), request, String.class);
        return response.getBody();
    }

    @Override
    public String deleteOrders(Integer ordersId, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("ordersId", ordersId);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("delete"), request, String.class);
        return response.getBody();
    }

    @Override
    public String addOrdersLogistics(Integer ordersId, String logistics, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("ordersId", ordersId);
        map.add("logistics", logistics);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("addOrdersLogistics"), request, String.class);
        return response.getBody();
    }

    @Override
    public String showAll(String context, Integer currentPage, Integer pageSize, String sort, Boolean asc, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("context", context);
        map.add("currentPage", currentPage);
        map.add("pageSize", pageSize);
        map.add("sort", sort);
        map.add("asc", asc);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("showForBusiness"), request, String.class);
        return response.getBody();
    }

    @Override
    public String showAllForUser(String context, Integer currentPage, Integer pageSize, String sort, Boolean asc, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("context", context);
        map.add("currentPage", currentPage);
        map.add("pageSize", pageSize);
        map.add("sort", sort);
        map.add("asc", asc);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("showForUser"), request, String.class);
        return response.getBody();
    }

    @Override
    public String showOrderForUser(Integer ordersId, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("ordersId", ordersId);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("showOne"), request, String.class);
        return response.getBody();
    }

    @Override
    public String addOrders(Integer goodsNum, Integer goodsId, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("goodsNum", goodsNum);
        map.add("goodsId", goodsId);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("add"), request, String.class);
        return response.getBody();
    }

    @Override
    public String addAddress(Integer ordersId, Integer addressId, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("ordersId", ordersId);
        map.add("addressId", addressId);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("addAddress"), request, String.class);
        return response.getBody();
    }

    @Override
    public String confirmGet(Integer ordersId, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("ordersId", ordersId);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("confirm"), request, String.class);
        return response.getBody();
    }
}
